package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasureshop;
import yxy.cherry.data.bean.charge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouritePackage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.*;

/**
 * 国庆活动商城信息
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayShopInfo
 * @Author jingtao-zheng
 * @Date 2022/9/20 16:09
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayShopInfo, classtype = NationalDayMessage.NationalDayShopInfoRequest.class)
public class CMD_Active_国庆寻宝_NationalDayShopInfo extends Command<NationalDayMessage.NationalDayShopInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayShopInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayShopInfoResponse.Builder response = NationalDayMessage.NationalDayShopInfoResponse.newBuilder();
        ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
        try {
            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            //商品表
            Map<String, acttreasureshop> map = DataCenter.getAllData(acttreasureshop.class);

            //返回商品信息
            ArrayList<NationalDayMessage.NationalDayShop> list = getFavouritePackage(am, map);

            response.addAllShop(list);

            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            //返回
            response.setBeginTime(startTime);
            response.setEndTime(endTime);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    private void send(MessageSource messageSource, NationalDayMessage.NationalDayShopInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    /**
     * 获取商品信息
     * @param am
     * @param map
     * @return
     */
    private ArrayList<NationalDayMessage.NationalDayShop> getFavouritePackage(ActiveModule_国庆寻宝 am, Map<String, acttreasureshop> map) {

        ArrayList<NationalDayMessage.NationalDayShop> list = new ArrayList<>();

        for (FavouritePackage fp : am.getDgifts()) {

            NationalDayMessage.NationalDayShop.Builder build = NationalDayMessage.NationalDayShop.newBuilder();

            acttreasureshop shopdata = map.get(String.valueOf(fp.getId()));

            build.setId(shopdata.getId());//表id
            build.setGoodsid(shopdata.getPurchasever());//商品id
            build.setLimitnum(shopdata.getNumber());//限购数量
            build.setBuynum(fp.getBuyNum());//购买的数量
            Currencies curs = new Currencies(Currency.create(shopdata.getRewardtype(), shopdata.getRewardid(), shopdata.getRewardnum())); //奖励
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            build.addAllCurs(items);//商品物品
            charge charge = DataCenter.getData(fp.getGoodsid(), yxy.cherry.data.bean.charge.class);
            build.setMoney(charge.getMoney());//价格

            //是否可购买：通过判断购买次数是否>=限购次数
            int canget = fp.getBuyNum() >= fp.getNumlimit() ? DrawStatus.cannotget.getStatus() : DrawStatus.canget.getStatus();
            build.setCanget(canget);//是否可购买
            list.add(build.build());
        }

        //从低到高排序
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                NationalDayMessage.NationalDayShop builder1 = (NationalDayMessage.NationalDayShop) o1;
                NationalDayMessage.NationalDayShop builder2 = (NationalDayMessage.NationalDayShop) o2;
                return builder1.getMoney() - builder2.getMoney();
            }
        });

        return list;
    }
}
